import { defineStore,createPinia } from "pinia";
import { Customer as CustomerInterface,CustomerInfo } from "@/stores/interface";
import piniaPersistConfig from "@/config/piniaPersist";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
// AuthStore
export {type CustomerInfo};
export const Customer = defineStore({
	id: "Customer",
	state: (): CustomerInterface => ({
		list: [],
		user_id:0,
		selCustomer:{
			name:"",
			phone:"",
			stature:"",
			weight:"",
			user_id:0  //大于0时为选中客户
		}
	}),
	getters: {
		getList: state => state.list,
		isPick:state => state.selCustomer.user_id !== 0  ,
		selInfo:state => state.selCustomer ,
	},
	actions: {
		async add(info:CustomerInfo) {
			this.user_id++;
			info.user_id = this.user_id;
		    this.list.unshift(info)	;
		},
		async del(index:number) {
			this.list.splice(index,1);
		},

		async save(index:number,info:CustomerInfo) {
			 this.list[index] = info;
		},
		async pickUp(info:CustomerInfo) {
			 this.selCustomer = info;
		},
		async outCustomer() {
			this.selCustomer.user_id = 0;
	   },
	},
	persist: piniaPersistConfig("Customer")
});

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

export default pinia;